package view_server

import (
	"bigdevops/src/web/middleware"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
)

// 配置路由
func ConfigRoutes(r *gin.Engine) {

	// 定义一个组
	// 共享中间件
	base := r.Group("/")
	{
		base.GET("/ping", ping)
		base.GET("/now", GetNowTs)
		base.GET("/long", longRequest)
		base.POST("/login", UserLogin)

	}

	noAuth := r.Group("/noAuth")
	{
		// 获取配置文件 大部分给 prometheus用的
		noAuth.GET("/downLoadPrometheusMainConfigYaml", downLoadPrometheusMainConfigYaml)
		noAuth.GET("/downLoadAlertManagerMainConfigYaml", downLoadAlertManagerMainConfigYaml)
		noAuth.GET("/downLoadPrometheusAlertConfigYaml", downLoadPrometheusAlertConfigYaml)
		noAuth.GET("/downLoadPrometheusRecordConfigYaml", downLoadPrometheusRecordConfigYaml)
		// http类型的 服务发现接口
		noAuth.GET("/getLeafStreeNodeBindIps", getLeafStreeNodeBindIps)
		// 临时测试的

		noAuth.GET("/getMonitorOndutyGroupOneFuturePlan/:id", getMonitorOndutyGroupOneFuturePlan)

		// websocket使用的
		noAuth.GET("/getPodContainerTailLog", getPodContainerTailLog)
		noAuth.GET("/getPodContainerWebShell", getPodContainerWebShell)
		noAuth.POST("/createK8sInstanceOne", createK8sInstanceOne)
		noAuth.POST("/updateK8sInstanceOne", updateK8sInstanceOne)
		noAuth.DELETE("/batchDeleteK8sInstance", batchDeleteK8sInstance)
		noAuth.POST("/batchRestartK8sInstance", batchRestartK8sInstance)
		noAuth.GET("/getK8sInstanceByApp", getK8sInstanceByApp)
		noAuth.POST("/updateK8sAppOne", updateK8sAppOne)
		noAuth.DELETE("/deleteK8sAppOne/:id", deleteK8sAppOne)

		noAuth.GET("/getK8sAppOneByClusterAndName", getK8sAppOneByClusterAndName)

	}

	// 下面的group 都要求有认证了
	afterLoginApiGroup := r.Group("/api")
	afterLoginApiGroup.Use(middleware.JWTAuthMiddleware()).Use(middleware.CasbinRbacMiddleware())
	{
		afterLoginApiGroup.GET("/getUserInfo", getUserInfoAfterLogin)
		afterLoginApiGroup.GET("/getPermCode", getPermCode)
	}

	// 底座模块
	systemApiGroup := afterLoginApiGroup.Group("/system")
	{
		// 菜单相关
		systemApiGroup.GET("/getMenuList", getMenuList)
		systemApiGroup.GET("/getMenuListAll", getMenuListAll)
		systemApiGroup.POST("/updateMenu", updateMenu)
		systemApiGroup.POST("/createMenu", createMenu)
		systemApiGroup.DELETE("/deleteMenu/:id", deleteMenu)

		// 角色相关
		systemApiGroup.GET("/getRoleListAll", getRoleListAll)
		systemApiGroup.POST("/createRole", createRole)
		systemApiGroup.POST("/updateRole", updateRole)
		systemApiGroup.POST("/setRoleStatus", setRoleStatus)
		systemApiGroup.DELETE("/deleteRole/:id", deleteRole)

		// 用户相关
		systemApiGroup.POST("/createAccount", createAccount)
		systemApiGroup.POST("/accountExist", accountExist)
		systemApiGroup.POST("/updateAccount", updateAccount)
		systemApiGroup.POST("/changePassword", changePassword)
		systemApiGroup.GET("/getAccountList", getAccountList)
		systemApiGroup.GET("/getAllUserAndRoles", getAllUserAndRoles)
		systemApiGroup.DELETE("/deleteAccount/:id", deleteAccount)

		// api 相关
		systemApiGroup.GET("/getApiList", getApiList)
		systemApiGroup.GET("/getApiListAll", getApiListAll)
		systemApiGroup.DELETE("/deleteApi/:id", deleteApi)
		systemApiGroup.POST("/createApi", createApi)
		systemApiGroup.POST("/updateApi", updateApi)

	}
	// 服务树模块
	streeApiGroup := afterLoginApiGroup.Group("/stree")
	{
		streeApiGroup.GET("/getStreeNodeList", getStreeNodeList)
		streeApiGroup.GET("/getStreeNodeSelect", getStreeNodeSelect)
		streeApiGroup.GET("/getStreeNodeSelectWithCicdItem", getStreeNodeSelectWithCicdItem)
		//streeApiGroup.GET("/getTopStreeNodes", getTopStreeNodes)
		streeApiGroup.GET("/getTopStreeNodes", getTopStreeNodesUseCache)
		streeApiGroup.GET("/getLeafStreeNodes", getLeafStreeNodes)
		streeApiGroup.POST("/createStreeNode", createStreeNode)
		streeApiGroup.DELETE("/deleteStreeNode/:id", deleteStreeNode)
		streeApiGroup.GET("/getChildrenStreeNodes/:pid", getChildrenStreeNodes)
		streeApiGroup.POST("/updateStreeNode", updateStreeNode)

		streeApiGroup.GET("/getResourceEcsUnbindList", getResourceEcsUnbindList)
		streeApiGroup.GET("/getResourceEcslList", getResourceEcslList)
		streeApiGroup.GET("/getResourceElbUnbindList", getResourceElbUnbindList)
		streeApiGroup.GET("/getResourceRdsUnbindList", getResourceRdsUnbindList)
		streeApiGroup.POST("/bindEscToStreeNode", bindEscToStreeNode)
		streeApiGroup.POST("/bindElbToStreeNode", bindElbToStreeNode)
		streeApiGroup.POST("/bindRdsToStreeNode", bindRdsToStreeNode)
		streeApiGroup.POST("/unBindEscToStreeNode", unBindEscToStreeNode)
		streeApiGroup.POST("/unBindElbToStreeNode", unBindElbToStreeNode)
		streeApiGroup.POST("/unBindRdsToStreeNode", unBindRdsToStreeNode)
		streeApiGroup.GET("/fetchResourceByNode", fetchResourceByNode)

	}

	// 工单模块
	workOrderApiGroup := afterLoginApiGroup.Group("/workorder")
	{
		workOrderApiGroup.POST("/createProcess", createProcess)
		workOrderApiGroup.POST("/updateProcess", updateProcess)
		workOrderApiGroup.GET("/getProcessList", getProcessList)
		workOrderApiGroup.DELETE("/deleteProcess/:id", deleteProcess)

		workOrderApiGroup.POST("/createFormDesign", createFormDesign)
		workOrderApiGroup.POST("/updateFormDesign", updateFormDesign)
		workOrderApiGroup.GET("/getFormDesignList", getFormDesignList)

		workOrderApiGroup.DELETE("/deleteFormDesign/:id", deleteFormDesign)

		workOrderApiGroup.POST("/createWorkOrderTemplate", createWorkOrderTemplate)
		workOrderApiGroup.POST("/updateWorkOrderTemplate", updateWorkOrderTemplate)

		workOrderApiGroup.GET("/getWorkOrderTemplateList", getWorkOrderTemplateList)

		workOrderApiGroup.DELETE("/deleteWorkOrderTemplate/:id", deleteWorkOrderTemplate)
		workOrderApiGroup.GET("/getWorkOrderTemplateDetail/:id", getWorkOrderTemplateDetail)

		workOrderApiGroup.POST("/createWorkOrderInstance", createWorkOrderInstance)
		workOrderApiGroup.POST("/approvalWorkOrderInstance/:id", approvalWorkOrderInstance)
		workOrderApiGroup.POST("/actionWorkOrderInstance/:id", actionWorkOrderInstance)
		workOrderApiGroup.POST("/commentWorkOrderInstance/:id", commentWorkOrderInstance)

		workOrderApiGroup.GET("/getWorkOrderInstanceList", getWorkOrderInstanceList)
		workOrderApiGroup.GET("/getWorkOrderInstanceOne/:id", getWorkOrderInstanceOne)

	}

	// 任务执行模块
	jobExecApiGroup := afterLoginApiGroup.Group("/jobexec")
	{
		jobExecApiGroup.GET("/getJobExecScriptList", getJobExecScriptList)
		jobExecApiGroup.GET("/getJobExecScriptSelect", getJobExecScriptSelect)
		jobExecApiGroup.GET("/getJobExecScriptOne/:id", getJobExecScriptOne)
		jobExecApiGroup.POST("/createJobExecScript", createJobExecScript)
		jobExecApiGroup.POST("/updateJobExecScript", updateJobExecScript)
		jobExecApiGroup.DELETE("/deleteJobExecScript/:id", deleteJobExecScript)
		jobExecApiGroup.GET("/getJobTaskOne/:id", getJobTaskOne)

		jobExecApiGroup.GET("/getJobTaskList", getJobTaskList)
		jobExecApiGroup.POST("/createJobTask", createJobTask)
		jobExecApiGroup.POST("/actionJobTaskOne/:id", actionJobTaskOne)
		jobExecApiGroup.GET("/getJobExecResultByJobId", getJobExecResultByJobId)

	}
	// 监控模块
	moniotrApiGroup := afterLoginApiGroup.Group("/monitor")
	{

		// 采集池和采集job
		moniotrApiGroup.GET("/getMonitorScrapePoolList", getMonitorScrapePoolList)
		moniotrApiGroup.POST("/createMonitorScrapePool", createMonitorScrapePool)
		moniotrApiGroup.POST("/updateMonitorScrapePool", updateMonitorScrapePool)
		moniotrApiGroup.GET("/getMonitorPrometheusYamlOne", getMonitorPrometheusYamlOne)
		moniotrApiGroup.GET("/getMonitorPrometheusAlertYamlOne", getMonitorPrometheusAlertYamlOne)
		moniotrApiGroup.GET("/getMonitorPrometheusRecordYamlOne", getMonitorPrometheusRecordYamlOne)
		moniotrApiGroup.GET("/getMonitorAlertMangerYamlOne", getMonitorAlertMangerYamlOne)
		moniotrApiGroup.DELETE("/deleteMonitorScrapePool/:id", deleteMonitorScrapePool)
		moniotrApiGroup.GET("/getMonitorScrapeJobList", getMonitorScrapeJobList)
		moniotrApiGroup.POST("/createMonitorScrapeJob", createMonitorScrapeJob)
		moniotrApiGroup.POST("/updateMonitorScrapeJob", updateMonitorScrapeJob)

		// 值班组
		moniotrApiGroup.GET("/getMonitorOndutyGroupList", getMonitorOndutyGroupList)
		moniotrApiGroup.POST("/createMonitorOndutyGroup", createMonitorOndutyGroup)
		moniotrApiGroup.POST("/createMonitorOndutyGroupChange", createMonitorOndutyGroupChange)
		moniotrApiGroup.POST("/updateMonitorOndutyGroup", updateMonitorOndutyGroup)
		moniotrApiGroup.DELETE("/deleteMonitorOndutyGroup/:id", deleteMonitorOndutyGroup)
		moniotrApiGroup.GET("/getMonitorOndutyGroupOne/:id", getMonitorOndutyGroupOne)
		moniotrApiGroup.GET("/getMonitorOndutyGroupOneFuturePlan/:id", getMonitorOndutyGroupOneFuturePlan)

		// alertmanager 集群
		moniotrApiGroup.GET("/getMonitorAlertmanagerPoolList", getMonitorAlertmanagerPoolList)
		moniotrApiGroup.POST("/createMonitorAlertmanagerPool", createMonitorAlertmanagerPool)
		moniotrApiGroup.POST("/updateMonitorAlertmanagerPool", updateMonitorAlertmanagerPool)
		moniotrApiGroup.DELETE("/deleteMonitorAlertmanagerPool/:id", deleteMonitorAlertmanagerPool)

		// 发送组
		moniotrApiGroup.GET("/getMonitorSendGroupList", getMonitorSendGroupList)
		moniotrApiGroup.POST("/createMonitorSendGroup", createMonitorSendGroup)
		moniotrApiGroup.POST("/updateMonitorSendGroup", updateMonitorSendGroup)
		moniotrApiGroup.DELETE("/deleteMonitorSendGroup/:id", deleteMonitorSendGroup)

		// 告警规则
		moniotrApiGroup.GET("/getMonitorAlertRuleList", getMonitorAlertRuleList)
		moniotrApiGroup.GET("/promqlExprCheck", promqlExprCheck)
		moniotrApiGroup.POST("/createMonitorAlertRule", createMonitorAlertRule)
		moniotrApiGroup.POST("/updateMonitorAlertRule", updateMonitorAlertRule)
		moniotrApiGroup.POST("/enableSwitchMonitorAlertRule/:id", enableSwitchMonitorAlertRule)
		moniotrApiGroup.POST("/batchEnableSwitchMonitorAlertRule", batchEnableSwitchMonitorAlertRule)
		moniotrApiGroup.DELETE("/deleteMonitorAlertRule/:id", deleteMonitorAlertRule)
		moniotrApiGroup.DELETE("/batchDeleteMonitorAlertRule", batchDeleteMonitorAlertRule)

		// 告警事件
		moniotrApiGroup.GET("/getMonitorAlertEventList", getMonitorAlertEventList)
		moniotrApiGroup.POST("/eventAlertSilence/:id", eventAlertSilence)
		moniotrApiGroup.POST("/eventAlertRenLing/:id", eventAlertRenLing)
		moniotrApiGroup.POST("/eventAlertUnSilence/:id", eventAlertUnSilence)
		moniotrApiGroup.POST("/batchEventAlertSilence", batchEventAlertSilence)

		// 预聚合
		moniotrApiGroup.GET("/getMonitorRecordRuleList", getMonitorRecordRuleList)
		moniotrApiGroup.POST("/createMonitorRecordRule", createMonitorRecordRule)
		moniotrApiGroup.POST("/updateMonitorRecordRule", updateMonitorRecordRule)
		moniotrApiGroup.DELETE("/deleteMonitorRecordRule/:id", deleteMonitorRecordRule)
		moniotrApiGroup.DELETE("/batchDeleteMonitorRecordRule", batchDeleteMonitorRecordRule)
		moniotrApiGroup.POST("/enableSwitchMonitorRecordRule/:id", enableSwitchMonitorRecordRule)
		moniotrApiGroup.POST("/batchEnableSwitchMonitorRecordRule", batchEnableSwitchMonitorRecordRule)
	}

	// k8s模块 管理员
	k8sApiGroup := afterLoginApiGroup.Group("/k8s")
	{
		// 集群
		k8sApiGroup.GET("/getK8sClusterList", getK8sClusterList)
		k8sApiGroup.GET("/getK8sClusterListForSelect", getK8sClusterListForSelect)
		k8sApiGroup.POST("/createK8sCluster", createK8sCluster)
		k8sApiGroup.POST("/updateK8sCluster", updateK8sCluster)
		// 节点
		k8sApiGroup.GET("/getK8sNodeList", getK8sNodeList)
		k8sApiGroup.GET("/taintYamlCheck", taintYamlCheck)
		k8sApiGroup.POST("/scheduleEnableSwitchK8sNodes", scheduleEnableSwitchK8sNodes)
		k8sApiGroup.POST("/labelK8sNodes", labelK8sNodes)
		k8sApiGroup.POST("/taintsK8sNodes", taintsK8sNodes)
		k8sApiGroup.POST("/drainK8sNodes", drainK8sNodes)
		k8sApiGroup.GET("/getK8sNodeDetail", getK8sNodeDetail)
		k8sApiGroup.GET("/getPodsListByNodeName", getPodsListByNodeName)

		// yaml
		k8sApiGroup.GET("/getK8sYamlTemplateList", getK8sYamlTemplateList)
		k8sApiGroup.POST("/createK8sYamlTemplate", createK8sYamlTemplate)
		k8sApiGroup.POST("/updateK8sYamlTemplate", updateK8sYamlTemplate)
		k8sApiGroup.DELETE("/deleteK8sYamlTemplate/:id", deleteK8sYamlTemplate)

		k8sApiGroup.GET("/getK8sYamlTaskList", getK8sYamlTaskList)
		k8sApiGroup.POST("/createK8sYamlTask", createK8sYamlTask)
		k8sApiGroup.POST("/updateK8sYamlTask", updateK8sYamlTask)
		k8sApiGroup.POST("/applyK8sYamlTaskOne/:id", applyK8sYamlTaskOne)
		k8sApiGroup.DELETE("/deleteK8sYamlTask/:id", deleteK8sYamlTask)

		// ns
		k8sApiGroup.GET("/getClusterNamespacesForCascader", getClusterNamespacesForCascader)

		k8sApiGroup.GET("/getClusterNamespacesForSelect", getClusterNamespacesForSelect)

		// pod
		k8sApiGroup.GET("/getK8sPodListByNs", getK8sPodListByNs)

		k8sApiGroup.GET("/getK8sPodContainers", getK8sPodContainers)
		k8sApiGroup.GET("/getPodContainerNormalLog", getPodContainerNormalLog)
		k8sApiGroup.GET("/getK8sPodYamlOne", getK8sPodYamlOne)

		// deploy
		k8sApiGroup.GET("/getK8sDeployListByNs", getK8sDeployListByNs)
		k8sApiGroup.POST("/setDeployOneContainerImage", setDeployOneContainerImage)
		k8sApiGroup.POST("/k8sScaleDeployOne", k8sScaleDeployOne)
		k8sApiGroup.DELETE("/k8sBatchDeleteDeploy", k8sBatchDeleteDeploy)
		k8sApiGroup.POST("/k8sBatchRestartDeploy", k8sBatchRestartDeploy)
		k8sApiGroup.GET("/getK8sDeployYamlOne", getK8sDeployYamlOne)

		// configmap
		k8sApiGroup.GET("/getK8sConfigmapListByNs", getK8sConfigmapListByNs)
		k8sApiGroup.POST("/k8sUpdateConfigmapDataOne", k8sUpdateConfigmapDataOne)
		k8sApiGroup.GET("/getK8sConfigmapYamlOne", getK8sConfigmapYamlOne)
		k8sApiGroup.DELETE("/k8sBatchDeleteConfigmap", k8sBatchDeleteConfigmap)

		//	service
		k8sApiGroup.GET("/getK8sServiceListByNs", getK8sServiceListByNs)
		k8sApiGroup.GET("/getK8sServiceYamlOne", getK8sServiceYamlOne)
		k8sApiGroup.POST("/k8sCreateOrUpdateServiceOne", k8sCreateOrUpdateServiceOne)
		k8sApiGroup.DELETE("/k8sBatchDeleteService", k8sBatchDeleteService)

	}

	// k8s 普通运维
	k8sAppApiGroup := afterLoginApiGroup.Group("/k8sApp")

	{

		// ns

		k8sAppApiGroup.GET("/getClusterNamespacesUnique", getClusterNamespacesUnique)
		// instance
		k8sAppApiGroup.POST("/createK8sInstanceOne", createK8sInstanceOne)
		k8sAppApiGroup.POST("/updateK8sInstanceOne", updateK8sInstanceOne)
		k8sAppApiGroup.POST("/createOrUpdateK8sInstanceOneForCICD", createOrUpdateK8sInstanceOneForCICD)
		k8sAppApiGroup.DELETE("/batchDeleteK8sInstance", batchDeleteK8sInstance)
		k8sAppApiGroup.POST("/batchRestartK8sInstance", batchRestartK8sInstance)
		k8sAppApiGroup.GET("/getK8sInstanceByApp", getK8sInstanceByApp)
		k8sAppApiGroup.GET("/getK8sInstanceList", getK8sInstanceList)
		k8sAppApiGroup.GET("/getK8sInstanceOne/:id", getK8sInstanceOne)

		// app Deployment和svc的抽象
		k8sAppApiGroup.GET("/getK8sAppList", getK8sAppList)
		k8sAppApiGroup.POST("/createK8sAppOne", createK8sAppOne)
		k8sAppApiGroup.POST("/updateK8sAppOne", updateK8sAppOne)
		k8sAppApiGroup.DELETE("/deleteK8sAppOne/:id", deleteK8sAppOne)
		k8sAppApiGroup.GET("/getK8sAppOne/:id", getK8sAppOne)
		k8sAppApiGroup.GET("/getK8sPodListByDeploy", getK8sPodListByDeploy)
		k8sAppApiGroup.GET("/getK8sAppListForSelect", getK8sAppListForSelect)

		// project 应用的项目
		k8sAppApiGroup.GET("/getK8sProjectList", getK8sProjectList)
		k8sAppApiGroup.GET("/getK8sProjectListForSelect", getK8sProjectListForSelect)
		k8sAppApiGroup.POST("/createK8sProject", createK8sProject)
		k8sAppApiGroup.POST("/updateK8sProject", updateK8sProject)
		k8sAppApiGroup.DELETE("/deleteK8sProjectOne/:id", deleteK8sProjectOne)

		// cronjob
		k8sAppApiGroup.GET("/getK8sCronjobList", getK8sCronjobList)
		k8sAppApiGroup.POST("/createK8sCronjobOne", createK8sCronjobOne)
		k8sAppApiGroup.POST("/updateK8sCronjobOne", updateK8sCronjobOne)
		k8sAppApiGroup.GET("/getK8sCronjobOne/:id", getK8sCronjobOne)
		k8sAppApiGroup.GET("/getK8sCronjobLastPod/:id", getK8sCronjobLastPod)
		k8sAppApiGroup.DELETE("/batchDeleteK8sCronjob", batchDeleteK8sCronjob)

	}

	// cicd
	cicdApiGroup := afterLoginApiGroup.Group("/cicd")
	{

		// 服务发布项
		cicdApiGroup.GET("/getCicdDeployItemList", getCicdDeployItemList)
		cicdApiGroup.GET("/getCicdDeployItemByStreeNodeId/:id", getCicdDeployItemByStreeNodeId)
		cicdApiGroup.POST("/createCicdDeployItem", createCicdDeployItem)
		cicdApiGroup.POST("/updateCicdDeployItem", updateCicdDeployItem)
		cicdApiGroup.DELETE("/deleteCicdDeployItem/:id", deleteCicdDeployItem)

		// 工单
		cicdApiGroup.GET("/getCicdDeployWorkOrderList", getCicdDeployWorkOrderList)
		cicdApiGroup.POST("/createCicdDeployWorkOrder", createCicdDeployWorkOrder)
		cicdApiGroup.POST("/approvalCicdDeployWorkOrder/:id", approvalCicdDeployWorkOrder)
		cicdApiGroup.POST("/closeCicdDeployWorkOrder/:id", closeCicdDeployWorkOrder)
		cicdApiGroup.GET("/getCicdWorkOrderOne/:id", getCicdWorkOrderOne)

		// 发布阶段
		cicdApiGroup.POST("/actionCicdDeployStage/:id", actionCicdDeployStage)
		cicdApiGroup.POST("/rollBackCicdDeployStage/:id", rollBackCicdDeployStage)
		cicdApiGroup.POST("/approveRolloutCicdDeployStage/:id", approveRolloutCicdDeployStage)
		cicdApiGroup.GET("/getCicdDeployStageListByOrderId/:id", getCicdDeployStageListByOrderId)
		cicdApiGroup.GET("/getCicdDeployStagePodList/:id", getCicdDeployStagePodList)
		cicdApiGroup.POST("/updateTestResultCicdDeployStage/:id", updateTestResultCicdDeployStage)

		// gitlab
		cicdApiGroup.GET("/listGitlabProjects", listGitlabProjects)
		cicdApiGroup.GET("/listGitlabTagsByProjectId/:id", listGitlabTagsByProjectId)
		cicdApiGroup.GET("/listGitlabTagsByCicdItemId/:id", listGitlabTagsByCicdItemId)

	}

}

func GetNowTs(c *gin.Context) {
	c.String(200, time.Now().Format("2006-01-02 15:04:05"))
}

func longRequest(c *gin.Context) {
	fmt.Printf("longRequest请求开始，休息6秒\n :%v", c)
	time.Sleep(10 * time.Second)

	c.String(200, "ok")
}
